import { request } from '../../request/index.js';
import regeneratorRuntime from '../../lib/runtime/runtime.js';

Page({

  /**
   * 页面的初始数据
   */
  data: {
    leftMenuList: [],
    rightContent: [],
    currentIndex: 0,
    scrollTop: 0
  },
  CategoryData: [],
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    const store = wx.getStorageSync("category");
    if (!store) {
      this.getCategoryData()
    } else {
      if (Date.now() - store.time > 300000) {
        this.getCategoryData()
      } else {
        this.CategoryData = store.data
        let leftMenuList = this.CategoryData.map(item => item.cat_name)
        let rightContent = this.CategoryData[0].children
        this.setData({ leftMenuList, rightContent })
      }
    }


    // this.getCategoryData()
  },
  async getCategoryData() {
    // request({ url: '/categories' })
    //   .then(result => {
    //     this.CategoryData = result.data.message

    //     wx.setStorageSync("category", {
    //       time: Date.now(),
    //       data: this.CategoryData
    //     });

    //     let leftMenuList = this.CategoryData.map(item => item.cat_name)
    //     let rightContent = this.CategoryData[0].children
    //     this.setData({ leftMenuList, rightContent })
    //   })
    const result = await request({ url: '/categories' })
    this.CategoryData = result
    wx.setStorageSync("category", {
      time: Date.now(),
      data: this.CategoryData
    });
    let leftMenuList = this.CategoryData.map(item => item.cat_name)
    let rightContent = this.CategoryData[0].children
    this.setData({ leftMenuList, rightContent })
  },
  handlerItem(e) {
    const { index } = e.currentTarget.dataset
    let rightContent = this.CategoryData[index].children
    this.setData({
      currentIndex: index,
      rightContent,
      scrollTop: 0
    })
  }

})